CC?=gcc
# C代码编译选项 (不使用 Zbb 扩展)
CFLAGS_PURE = -O0 -Wall -Wextra -I. -DHITLS_CRYPTO_SHA256 -DHITLS_CRYPTO_SHA512 -static

# 汇编代码编译选项 (使用 Zbb 扩展)
ASFLAGS_ZBB = -march=rv64gc_zbb -mabi=lp64d

LDFLAGS = -lm -static

# 源文件
C_SOURCES = test_sha256_performance.c noasm_sha256.c noasm_sha512.c
ASM_SOURCES = sha2_256_riscv64.S sha2_512_riscv64.S

# 目标文件
C_OBJECTS = $(C_SOURCES:.c=.o)
ASM_OBJECTS = $(ASM_SOURCES:.S=.o)
OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)

TARGET = sha2_perf_test

# 默认目标
all: $(TARGET)

# 编译规则
$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS)

# C 文件编译规则 (不使用 Zbb)
%.o: %.c
	$(CC) $(CFLAGS_PURE) -c $< -o $@

# 汇编文件编译规则 (使用 Zbb)
%.o: %.S
	$(CC) $(ASFLAGS_ZBB) -c $< -o $@

# 编译时显示详细信息
verbose: CFLAGS_PURE += -v
verbose: ASFLAGS_ZBB += -v
verbose: $(TARGET)

# 只编译 SHA256 测试
sha256_only: test_sha256_only.o noasm_sha256.o sha2_256_riscv64.o
	$(CC) $^ -o sha256_test $(LDFLAGS)

# 只编译 SHA512 测试
sha512_only: test_sha512_only.o noasm_sha512.o sha2_512_riscv64.o
	$(CC) $^ -o sha512_test $(LDFLAGS)

# 生成汇编代码查看
asm: noasm_sha256.c noasm_sha512.c
	$(CC) $(CFLAGS_PURE) -S noasm_sha256.c -o noasm_sha256.s
	$(CC) $(CFLAGS_PURE) -S noasm_sha512.c -o noasm_sha512.s

# 检查编译器支持的扩展
check_extensions:
	$(CC) -march=rv64gc_zbb -Q --help=target | grep -E "(zbb|enabled)"

# 清理
clean:
	rm -f $(OBJECTS) $(TARGET) *.s sha256_test sha512_test perf.data*

# 调试信息
debug:
	@echo "C_SOURCES: $(C_SOURCES)"
	@echo "ASM_SOURCES: $(ASM_SOURCES)"
	@echo "C_OBJECTS: $(C_OBJECTS)"
	@echo "ASM_OBJECTS: $(ASM_OBJECTS)"
	@echo "CFLAGS_PURE: $(CFLAGS_PURE)"
	@echo "ASFLAGS_ZBB: $(ASFLAGS_ZBB)"

.PHONY: all verbose sha256_only sha512_only asm check_extensions clean debug